﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Web.SessionState;

namespace SES.CMS
{
    public partial class ThongTin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                load();
                loadNB();
            }
        }

        public void initForm()
        {

        }

        public void initLabel()
        {

        }
        public void initUpdate()
        {
            txtDC.Visible = true;
            txtDT.Visible = true;
            txtEmail.Visible = true;
            txtHT.Visible = true;
            txtMobile.Visible = true;

            lbDC.Visible = false;
            lbEmail.Visible = false;
            lbHT.Visible = false;
            lbMobile.Visible = false;
            lbDT.Visible = false;
        }

        private void load()
        {
            if (Session["IDDN"] != null)
            {
                initForm();
                ClientLoginDO obj = new ClientLoginBL().Select(new ClientLoginDO { ClientLoginID = int.Parse(Session["IDDN"].ToString()) });


                lblDN.Text = obj.Username;
                lbHT.Text = obj.ClientName;
                lbDT.Text = obj.Phone;
                lbMobile.Text = obj.Mobile;
                lbEmail.Text = obj.Email;
                lbDC.Text = obj.Address;

                txtDC.Text = obj.Address;
                txtDT.Text = obj.Phone;
                txtEmail.Text = obj.Email;
                txtMobile.Text = obj.Mobile;
                txtHT.Text = obj.ClientName;
                Page.Title = "Thông tin cá nhân - " + obj.ClientName + " | Văn phòng phẩm Mỹ Đình";
            }
            else
            {
                Response.Redirect("/Default.aspx");
            }
        }

        public ClientLoginDO initObj()
        {
            ClientLoginDO obj = new ClientLoginDO();
            obj = new ClientLoginBL().Select(new ClientLoginDO { ClientLoginID = int.Parse(Session["IDDN"].ToString()) });
            return obj;
        }


        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            initUpdate();
            btnCapNhat.Visible = false;
            btnUpdate.Visible = true;
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ClientLoginDO obj = initObj();
            //if (txtHT.Visible == true)
            obj.ClientName = txtHT.Text;
            //else if (txtDC.Visible == true)
            obj.Address = txtDC.Text;
            //else if (txtDT.Visible == true)
            obj.Mobile = txtDT.Text;
            //else if (txtEmail.Visible == true)
            obj.Email = txtEmail.Text;
            obj.Mobile = txtMobile.Text;

            new ClientLoginBL().Update(obj);
            Ultility.Alert("Cập nhật thành công!", Request.Url.ToString());
            btnUpdate.Visible = false;
            btnCapNhat.Visible = true;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx");
        }
        public void loadNB()
        {
            bindatalist(new cmsSanPhamBL().SelectAllBanChay());
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rpt.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 6;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rpt.Visible = true;
                rpt.DataSource = objPds;
                rpt.DataBind();
            }
            else
            {
                lbThongbao.Visible = true;
                lbThongbao.Text = "Không có sản phẩm!";
            }
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            loadNB();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            loadNB();
        }
        public cmsGioHangDO init()
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            if (Session["ClientLoginID"] == null)
                obj.ClientLoginID = 0;
            else
                obj.ClientLoginID = int.Parse(Session["ClientLoginID"].ToString());
            obj.NgayDatHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.NgayGiaoHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.SoLuong = 1;
            obj.XuLy = false;
            return obj;
        }
        protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string url = Request.Url.AbsolutePath;
            try
            {
                HttpSessionState ss = HttpContext.Current.Session;
                string session = ss.SessionID;
                Int64 id = Int64.Parse(e.CommandArgument.ToString());
                cmsGioHangDO obj = new cmsGioHangBL().SelecBySessionAndSPNonXL(session, id);
                cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = id });
                float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);
                if (obj.GioHangID < 1)
                {
                    obj = init();
                    obj.SessionID = session;
                    obj.SanPhamID = id;
                    if (Session["IDDN"] == null)
                        obj.ClientLoginID = 0;
                    else
                        obj.ClientLoginID = int.Parse(Session["IDDN"].ToString());

                    obj.ThanhTien = gia * obj.SoLuong;
                    new cmsGioHangBL().Insert(obj);
                    Ultility.Alert("Đặt mua thành công!", url);
                }

                else
                {
                    obj.SoLuong++;
                    obj.ThanhTien += gia;
                    new cmsGioHangBL().Update(obj);
                    Ultility.Alert("Đặt mua thành công!", url);
                }
            }
            catch
            {
                Ultility.Alert("Lỗi!", url);
            }
        }
    }
}
